/* eslint-disable no-dupe-keys */ /* eslint-disable standard/no-callback-literal
*/ /* eslint-disable no-dupe-keys */ /* eslint-disable quotes */
<template>
  <div class="login-container">
    <div class="left">
      <div class="title-box">
        <img src="@/assets/login_icon.png" alt="" />
        <span class="title">黑马面面</span>
        <span class="line"></span>
        <span class="sub-title">用户登录</span>
      </div>
      <!-- form表单部分 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="phone" class="formInput">
          <el-input
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password" class="formInput">
          <el-input
            v-model="ruleForm.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-row class="captcha formInput">
          <el-col :span="16"
            ><div class="grid-content bg-purple">
              <el-form-item prop="code">
                <el-input
                  v-model="ruleForm.code"
                  placeholder="请输入密码"
                  prefix-icon="el-icon-key"
                ></el-input>
              </el-form-item></div
          ></el-col>
          <el-col :span="7">
            <div class="grid-content bg-purple-light">
              <img :src="captcha" @click="captchaCode" alt="" /></div
          ></el-col>
        </el-row>
        <!-- 协议签订 -->
        <el-form-item prop="checked" class="formInput">
          <el-checkbox v-model="ruleForm.checked"></el-checkbox>
          <span
            >我已经阅读并同意<a href="javascript:;">用户协议</a>和<a
              href="javascript:;"
              >隐私条款</a
            ></span
          >
        </el-form-item>
        <!-- 登录注册 -->
        <el-form-item>
          <div>
            <el-button
              type="primary"
              @click="submitForm('ruleForm')"
              style="width: 100%;"
            >
              登录
            </el-button>
          </div>
          <div>
            <el-button
              type="primary"
              @click="register()"
              style="width: 100%; margin-top: 30px;"
            >
              注册
            </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="right">
      <img src="@/assets/login_bg.png" alt="" />
    </div>
    <register ref="registerRef"></register>
  </div>
</template>
<script>
import register from './register'
import { saveToken } from '../../utils/token'
export default {
  components: {
    register
  },
  data () {
    return {
      dialogVisible: false,
      checked: true,
      captcha: process.env.VUE_APP_SECRET + '/captcha?type=login',
      ruleForm: {
        phone: '18511111111',
        password: '12345678',
        code: '',
        checked: true
      },
      rules: {
        phone: [
          // { required: true, message: '请输入正确的手机号码', trigger: 'blur' },
          // { min: 11, max: 11, message: '长度在 11个字符', trigger: 'blur' }
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('手机号不能为空'))
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不合法'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 6, max: 12, message: '请输入6-12位的数字', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入正确的验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '请输入正确的验证码', trigger: 'blur' }
        ],
        checked: [
          {
            validator: (rule, value, callback) => {
              // eslint-disable-next-line standard/no-callback-literal
              value ? callback() : callback(new Error('请勾选用户协议'))
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    captchaCode () {
      this.captcha =
        process.env.VUE_APP_SECRET + '/captcha?type=login&f=' + Date.now()
    },
    submitForm () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        // 登录
        const res = await this.$axios.post('/login', this.ruleForm)
        if (res.code === 200) {
          this.$message.success('登录成功')
          // 保存token
          // localStorage.setItem('heimamm66', res.data.data.token)
          saveToken(res.data.token)
          // 登录后跳转页面
          this.$router.push('/layout')
        } else {
          // 登录错误提示
          this.$message({
            message: res.message,
            type: 'error',
            duration: 1000
          })
          // 刷新图形码
          this.captchaCode()
        }
      })
    },
    register () {
      this.$refs.registerRef.dialogVisible = true
    }
  },
  created () {
    this.captcha =
      process.env.VUE_APP_SECRET + '/captcha?type=login&f=' + Date.now()
  }
}
</script>
<style lang="less" scoped>
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
  }
  .formInput {
    margin-top: 30px;
  }
}

.captcha img {
  width: 100%;
  height: 40px;
  margin-left: 10px;
}
</style>
